package com.itheima.reggie.service.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    //操作三个数据库
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Override
    public Page<Dish> getPage(String name, Integer pageNum, Integer pageSize) {
        //定义查询条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //判断是否为空
        wrapper.like(StringUtils.isNotEmpty(null) ,Dish::getName,name);
        //定义分页条件
        Page page = new Page<Dish>(pageNum,pageSize);
        page= dishMapper.selectPage(page,wrapper);
        //遍历得到的page 得到所有菜品 此时需要得到菜品分类
        List<Dish> dishList = page.getRecords();
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //查询没一个菜品对应的那个种类的id
                //1. 根据菜品的category_id从分类表查询分类名称
                //SELECT * FROM category WHERE id = '1397844263642378242'
                Category category = categoryMapper.selectById(dish.getCategoryId());
                //将查询后的菜品名字封装
                dish.setCategoryName(category.getName());

                //查询菜品对应的口味
                //SELECT * FROM `dish_flavor` WHERE dish_id = '1397849739276890114'
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                //判断菜品id 是否和dish 的id 相同 的到有个 菜品 品味组合
                List<DishFlavor> selectList = dishFlavorMapper.selectList(wrapper1);
                //把 组合封装到dish中
                dish.setFlavors(selectList);
            }
        }
        return page;
    }
    @Transactional //事务问题 多个sql 语句
    //添加方法
    @Override
    public void save(Dish dish) {
        //将dish 基本信息保存到菜品表
        dishMapper.insert(dish);
        //将对应的菜品口味保存到里边
        List<DishFlavor> flavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(flavorList)){
            for (DishFlavor dishFlavor : flavorList) {
                dishFlavor.setDishId(dish.getId());
                //保存一个口味
                dishFlavorMapper.insert(dishFlavor);
            }

    }
    }
    @Transactional
    @Override
    public Dish findById(Long id) {
        //通过id查找菜品id
        Dish dish=dishMapper.selectById(id);
        //通过id查找口味id 封装到dish中
        //查询菜品对应的口味
        //SELECT * FROM `dish_flavor` WHERE dish_id = '1397849739276890114'
        LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DishFlavor::getDishId,dish.getId());
        //判断菜品id 是否和dish 的id 相同 的到有个 菜品 品味组合
        List<DishFlavor> selectList = dishFlavorMapper.selectList(wrapper1);
        //把 组合封装到dish中
        dish.setFlavors(selectList);

        return dish;
    }
    @Transactional
    @Override
    public void update(Dish dish) {
        //根据逐渐主键更新菜单
        dishMapper.updateById(dish);
         //2. 根据菜品id删除口味表中相关口味
        //DELETE FROM `dish_flavor` WHERE dish_id = ?  条件删除
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorMapper.delete(wrapper);
        //见新查询的口味传入其中
        List<DishFlavor> dishFlavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(dishFlavorList)) {
            for (DishFlavor dishFlavor : dishFlavorList) {
                dishFlavor.setDishId(dish.getId());
                //保存一个口味
                dishFlavorMapper.insert(dishFlavor);
            }
        }

    }
    //删除方法
    @Override
    public void delete(Long[] ids) {
        //遍历传入的id数组
        for (Long id : ids) {
            //删除菜单
            dishMapper.deleteById(id);
            //定义删除口味
            LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DishFlavor::getDishId,id);
            dishFlavorMapper.delete(wrapper);
        }
    }
//禁售 起售方法
    @Override
    public void setStatus(Integer type, Long[] ids) {
        for (Long id : ids) {
            //查询所有 并设置停售状态
            Dish dish = dishMapper.selectById(id);
            dish.setStatus(type);
            //将状态更新到菜单中
            LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Dish::getId,id);
            dishMapper.update(dish,wrapper);
        }
    }
    //套餐菜品回显
    @Override
    public List<Dish> getListApp(Long categoryId, Integer status) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,categoryId);
        wrapper.eq(Dish::getStatus, status);
        List<Dish> dishList = dishMapper.selectList(wrapper);
        //得到的套餐信息 遍历数组 得到菜品口味
        if (CollectionUtil.isNotEmpty(dishList)){
            for (Dish dish : dishList) {
                //通过查找菜品id相对与 套餐id
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavors);
            }
        }
        return dishList;
    }
//菜单显示 套餐添加根据name模糊查询所有菜品名称
    @Override
    public List<Dish> getList(Long categoryId, Integer status, String name) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(categoryId!=null,Dish::getCategoryId,categoryId);
        wrapper.eq(status!=null,Dish::getStatus,status);
        if (StringUtils.isNotEmpty(name)){
            wrapper.like(Dish::getName,name);
        }
        List<Dish> dishList = dishMapper.selectList(wrapper);
        return dishList;
    }


}
